<?PHP
require_once 'include/Image.php';

$webRoot = dirname(dirname(__FILE__));
$url = "http://se.5yyxx.com";
$fid = 3;
$pageList = $url."/html/11001/list_1_*.html";
$firstpage = 1;
$lastpage = 10;
for ($i=$firstpage; $i<=$lastpage; $i++) {
    $rows = array();
    $page = str_replace('*', $i, $pageList);
    $listPageContent = file_get_contents($page);
    preg_match_all("/<li><span>[\d-]*<\/span><a href=\"([\S]+)\" target=\"_blank\">([^<>]+)<\/a><\/li>/", $listPageContent, $match, PREG_PATTERN_ORDER);
    $array = array();
    for ($j=0; $j<count($match[0]); $j++) {
        $array[] = array('title'=>$match[2][$j],
                         'url'=>$match[1][$j]);
    }
    foreach ($array as $value) {
        $pathinfo = pathinfo($value['url']);
        $insidePageContent = file_get_contents($url.$value['url']);
        //in the future, get "<DIV class=tpc_content id=read_tpc>...</DIV>" by sampleXml or grep_match
        //and remove filterImg functin
        $pic = array();
        preg_match_all("/src=\"(\S*)\"/", $insidePageContent, $match, PREG_PATTERN_ORDER);
        for ($z=0; $z<count($match[1]); $z++) {
            if (filterImg($match[1][$z])) {
                $pic[] = $match[1][$z];
            };
        }
        $row['title'] = $value['title'];
        $row['pic'] = $pic;
        $row['key'] = $pathinfo['filename'];
        $rows[] = $row;
    }
    $link = mysql_connect('localhost', 'root', '123456');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_query("SET NAMES UTF8");
    mysql_select_db('discuz');

    $count = count($rows);
    $c = 1;
    foreach ($rows as $row) {
        collectOneTopic($row);
        echo "status: $c/$count\n";
        $c++;
    }
}


function collectOneTopic($row)
{
    global $fid;
    $title = iconv("gb2312", "UTF-8", $row['title']);
    $pics = $row['pic'];
    $key = $row['key'];
    $sql = "INSERT INTO `collect_7bxx_europe_usa` ( `id` , `key_id` , `status`, `pic_url`, `fetch_time`, `discuz_pid` )VALUES (NULL , '".$key."', 'new', '".implode('#', $row['pic'])."', now(), NULL) ON DUPLICATE KEY UPDATE status='update';";
    if (mysql_query($sql) && 1 == mysql_affected_rows() && $cid = mysql_insert_id()) {
    
    $currentTime = time();
    
    $sql = "INSERT INTO `cdb_threads` (`tid`, `fid`, `iconid`, `typeid`, `readperm`, `price`, `author`, `authorid`, `subject`, `dateline`, `lastpost`, `lastposter`, `views`, `replies`, `displayorder`, `highlight`, `digest`, `rate`, `blog`, `special`, `attachment`, `subscribed`, `moderated`, `closed`, `itemid`, `supe_pushstatus`) VALUES 
(null, ".$fid.", 0, 0, 0, 0, 'admin', 1, '".$title."', ".$currentTime.", ".$currentTime.", 'admin', 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0);
";
    if (mysql_query($sql) && $tid = mysql_insert_id()) {
        $sql = "INSERT INTO `cdb_posts` (`pid`, `fid`, `tid`, `first`, `author`, `authorid`, `subject`, `dateline`, `message`, `useip`, `invisible`, `anonymous`, `usesig`, `htmlon`, `bbcodeoff`, `smileyoff`, `parseurloff`, `attachment`, `rate`, `ratetimes`, `status`) VALUES (null, ".$fid.", ".$tid.", 1, 'admin', 1, '".$title."', ".$currentTime.", 'prepare', '127.0.0.1', 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0);
";
        if (mysql_query($sql) && $pid = mysql_insert_id()) {
            $postContent = '';
            foreach ($pics as $picUrl) {
                $newNameInfo = generateNewName($picUrl);
                $newName = $newNameInfo['newName'];
                $picDir = getPicDir();
                $outputFile = $picDir['absolute'].'/'.$newName;
                exec('wget --timeout=10 --tries=3 --wait=3 -O '.$outputFile.' '.$picUrl, $outputtmp, $error);
                if ($error) {
                    continue;
                }
                //in the future deal if outer pregramme false how to do
                switch ($newNameInfo['ext']) {
                    case 'jpg':
                        $imageType = 'image/jpeg';
                        break;
                    case 'png':
                        $imageType = 'image/png';
                        break;
                    case 'gif':
                        $imageType = 'image/gif';
                        break;
                    default:
                        $imageType = '';
                }
                thumbImgCreate($outputFile);
                $filesize = @filesize($outputFile) ? filesize($outputFile) : '14';
                $sql = "INSERT INTO `cdb_attachments` (`aid`, `tid`, `pid`, `dateline`, `readperm`, `price`, `filename`, `description`, `filetype`, `filesize`, `attachment`, `downloads`, `isimage`, `uid`, `thumb`, `remote`) VALUES (null, ".$tid.", ".$pid.", ".$currentTime.", 0, 0, '".$newName."', '', '".$imageType."', '".$filesize."', '".$picDir['relative'].'/'.$newName."', 0, 1, 1, 1, 0);";
                if (mysql_query($sql) && $aid = mysql_insert_id()) {
                    $postContent .= "[attach]".$aid."[/attach]\n";
                }
            }
            $sql = "UPDATE `cdb_posts` SET `message` = '$postContent' WHERE `pid` =$pid";
            mysql_query($sql);
        }
    }
    $sql = 'UPDATE cdb_forums SET lastpost=\''.$tid.'\t'.$title.'\t'.$currentTime.'\tadmin\', threads=threads+1, posts=posts+1, todayposts=todayposts+1 WHERE fid=\''.$fid.'\'';
    mysql_query($sql) or mysql_error();
    $sql = 'UPDATE cdb_members SET posts=posts+(\'+1\') , lastpost=\''.$currentTime.'\'';
    mysql_query($sql) or mysql_error();
    $sql = 'UPDATE collect_7bxx_europe_usa SET discuz_pid=\''.$pid.'\' where id=\''.$cid.'\'';
    mysql_query($sql) or mysql_error();
   }    
}

function generateNewName($url)
{
    $info = pathinfo($url);
    $name = md5($info['filename'].date('His'));
    $rows = array();
    $ext = strtolower($info['extension']);
    $date = date('Ymd');
    $newName = $date.'_'.$name.'.'.$ext;
    return array('newName'=>$newName,
                 'ext'=>$ext);
}

function filterImg($fileUrl)
{
    $ext = pathinfo($fileUrl, PATHINFO_EXTENSION);
    if (in_array($ext, array('js'))) {
        return false;
    }
    if (strstr($fileUrl, 'happy9.com') or strstr($fileUrl, '9you.com')) {
        return false;
    }
    return true;
}

function getPicDir()
{
    global $webRoot;
    $attachSubdir = 'day_'.date('ymd');
    $attachDir = $webRoot.'/attachments/'.$attachSubdir;
    if(!is_dir($attachDir)) {
        @mkdir($attachDir, 0777);
        @fclose(fopen($attachDir.'/index.htm', 'w'));
    }
    return array('absolute'=>$attachDir,
                 'relative'=>$attachSubdir);
}

function thumbImgCreate($rawImg)
{
    $imghandle = FLEA_Helper_Image::createFromFile($rawImg);
    $imghandle->crop(120, 160);
    $imghandle->saveAsJpeg($rawImg.'.thumb.jpg');
}
